package com.example.zk2_lx;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.zk2_lx.adapter.ConsAdapter;
import com.example.zk2_lx.cons.ConsViewModel;
import com.example.zk2_lx.databinding.FragmentConsBinding;
import com.example.zk2_lx.mvvm_base.BaseFragment;
import com.hyphenate.chat.EMConversation;

import java.util.ArrayList;
import java.util.List;


public class ConsFragment extends BaseFragment<FragmentConsBinding, ConsViewModel> {

    List<EMConversation> list = new ArrayList<>();
    ConsAdapter consAdapter;


    public ConsFragment() {
        // Required empty public constructor
    }

    @Override
    protected int bindLayout() {
        return R.layout.fragment_cons;
    }

    @Override
    protected void initView() {
        consAdapter = new ConsAdapter(R.layout.item_cons,list);
        dataBinding.rv.setAdapter(consAdapter);
        dataBinding.rv.setLayoutManager(new LinearLayoutManager(getContext()));

        viewModel.getcons();

    }

    @Override
    protected void initData() {

        viewModel.success.observe(this, new Observer<List<EMConversation>>() {
            @Override
            public void onChanged(List<EMConversation> emConversations) {
                list.clear();
                Log.d("sss", "onChanged: "+emConversations.size());
                list.addAll(emConversations);
                consAdapter.notifyDataSetChanged();
            }
        });

    }

    @Override
    protected Class<ConsViewModel> getViewModel() {
        return ConsViewModel.class;
    }

    @Override
    public void onResume() {
        super.onResume();
        viewModel.getcons();
    }
}